package com.yunxin.core.domain.service.channel.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yunxin.core.domain.model.channel.ChannelPromotion;
import com.yunxin.core.domain.model.channel.ChannelPromotionBasicInfo;
import com.yunxin.core.domain.model.channel.ChannelPromotionBasicInfoRepository;
import com.yunxin.core.domain.model.channel.ChannelPromotionRepository;
import com.yunxin.core.domain.service.channel.ChannelPromotionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zero°
 * @since 2023-05-30
 */
@DS("core")
@Service
@RequiredArgsConstructor
public class ChannelPromotionServiceImpl implements ChannelPromotionService {

    private final ChannelPromotionRepository channelPromotionRepository;

    private final ChannelPromotionBasicInfoRepository channelPromotionBasicInfoRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addChannelPromotionBasicInfo(ChannelPromotion channelPromotion, ChannelPromotionBasicInfo basicInfo) {
        channelPromotionRepository.saveChannelPromotion(channelPromotion);
        basicInfo.setPromotionId(channelPromotion.getId());
        channelPromotionBasicInfoRepository.saveChannelPromotionBasicInfo(basicInfo);
        return channelPromotion.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateChannelPromotionBasicInfo(ChannelPromotion channelPromotion, ChannelPromotionBasicInfo basicInfo) {
        boolean result = channelPromotionRepository.updateChannelPromotion(channelPromotion);
        basicInfo.setPromotionId(channelPromotion.getId());
        boolean result1 = channelPromotionBasicInfoRepository.updateChannelPromotionBasicInfo(basicInfo);
        return result || result1;
    }

}
